<template>
    <div>
        <div class="adminSearch">
            <el-input v-model="keywords" placeholder="请输入用户名" prefix-icon="el-icon-search"></el-input>
            <el-button type="primary" icon="el-icon-search" @click="doSearch">搜索</el-button>
        </div>
        <div class="user-container">
            <el-card class="user-card" v-for="(user,index) in users" :key="index">
                <div slot="header" class="clearfix">
                    <span>{{user.name}}</span>
                    <el-button style="color: red" type="text" icon="el-icon-delete" @click="deleteUser(user)"></el-button>
                </div>
                <div class="userInfo-container">
                    <div>用户名：{{user.name}}</div>
                    <div>手机号码：{{user.phone}}</div>
                    <div>电话号码：{{user.telephone}}</div>
                    <div>地址：{{user.address}}</div>
                    <div>用户地址：
                        <el-switch
                                v-model="user.enabled"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                active-text="启用"
                                inactive-text="禁用"
                                @change="enableChange(user)"
                        >
                        </el-switch>
                    </div>
                    <div>
                        用户角色：
                        <el-tag type="suceess" v-for="(role,indexj) in user.roles" :key="indexj">
                            {{role.nameZh}}
                        </el-tag>
                        <el-popover
                                placement="right"
                                title="角色列表"
                                width="200"
                                trigger="click"
                                @show="showPop(user)"
                                @hide="hidePop(user)">
                            <el-select v-model="selectedRoles" multiple placeholder="请选择">
                                <el-option
                                        v-for="(r,index) in allRoles"
                                        :key="index"
                                        :label="r.namezh"
                                        :value="r.id">
                                </el-option>
                            </el-select>
                            <el-button slot="reference" type="text" icon="el-icon-more"></el-button>
                        </el-popover>
                    </div>
                    <div>
                        备注：{{user.remark}}
                    </div>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script>
    export default {
        name: "SysAdmin",
        data(){
            return{
                users:[],
                allRoles:[],
                selectedRoles:[],
                keywords:''
            }
        },
        mounted() {
            this.initUsers()
        },
        methods:{
            initUsers(){
                this.getRequest('/system/user/?keywords=' + this.keywords).then(resp => {
                    if (resp){
                        this.users = resp;
                    }
                })
            },
            initAllRoles(){
                this.getRequest('/system/user/roles').then(resp => {
                    if (resp){
                        this.allRoles = resp;
                    }
                })
            },
            doSearch(){
                this.initUsers();
            },
            deleteUser(user){
                this.$confirm('此操作将永久删除['+ user.name +']职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/user/' + user.id).then(resp =>{
                        if (resp){
                            this.initUsers();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            enableChange(user){
                this.putRequest('/user/info',user).then(resp => {
                    if (resp){
                        this.initUsers();
                    }
                })
            },
            showPop(user){
                this.initAllRoles();
                let roles = user.roles;
                this.selectedRoles = [];
                roles.forEach(r=>{
                    this.selectedRoles.push(r.id);
                });
            },
            hidePop(user){
                let flag = false;
                let roles = [];
                Object.assign(roles,user.roles);
                //长度变了，发送请求
                if(roles.length != this.selectedRoles.length){
                    flag = true;
                }else{
                    //长度不变，但数据变了，双循环比对然后发送请求
                    for (let i = 0;i<roles.length;i++){
                        let role = roles[i];
                        for (let j = 0; j < this.selectedRoles.length; j++){
                            let sr = this.selectedRoles[j];
                            if (role.id == sr){
                                roles.splice(i,1);
                                i--;
                                break;
                            }
                        }
                    }
                    if (roles.length != 0){
                        flag = true;
                    }
                }

                if (flag){
                    //拼接URL
                    let url = '/system/user/role?userId=' + user.id;
                    this.selectedRoles.forEach(rid => {
                        url += '&rids=' + rid;
                    });
                    this.putRequest(url).then(resp => {
                        if (resp){
                            this.initUsers();
                        }
                    })
                }
            }
        }
    }
</script>

<style>
    .adminSearch{
        display: flex;
        justify-content: center;
        margin-top: 10px;
    }
    .adminSearch .el-input{
        width: 400px;
        margin-right: 5px;
    }
    .user-container{
        display: flex;
        flex-wrap: wrap;
    }
    .user-card{
        width: 300px;
        margin-top:15px;
        margin-right: 20px;
    }
    .userInfo-container{
        font-size: 12px;
    }
    .userInfo-container .el-tag{
        margin-right: 5px;
        margin-top: 5px;
    }
</style>